//
//  BDMBaseOperationManager.h
//  welkin
//
//  Created by liyingpeng on 16/2/22.
//  Copyright © 2016年 liyingpeng. All rights reserved.
//

#import "AFHTTPRequestOperationManager.h"
#import "BDMHTTPRequestModel.h"
#import "BDMError.h"
#import "BDMUploadFileModel.h"
#import "BDMDownloadFileModel.h"

typedef void(^ConfigHeaderBlock)(NSMutableURLRequest * request, NSDictionary * aParameters, NSDictionary * aCustomerHeader);
typedef NSError *(^ConfigResultHandlerBlock)(id responseObject, BOOL isResponseJSONValid, NSInteger statusCode);
typedef NSCachedURLResponse * (^ConfigAdoptCachePolicyBlock)(NSMutableURLRequest * aRequest, NSCachedURLResponse * response, BOOL isResponseJSONValid);
typedef BDMHTTPRequestModel * (^ConfigRequestModelHandlerBlock)(BDMHTTPRequestModel *requestModel);


@interface BDMBaseOperationManager : AFHTTPRequestOperationManager

@property(nonatomic, copy) ConfigHeaderBlock configHeaderBlock;
@property(nonatomic, copy) ConfigResultHandlerBlock configResultHandlerBlock;
@property(nonatomic, copy) ConfigAdoptCachePolicyBlock configAdoptCachePolicyBlock;
@property(nonatomic, copy) ConfigRequestModelHandlerBlock configRequestModelBlock;


/*!
 @method
 @abstract 初始化缓存大小
 @discussion
 @result
 */
- (void)urlCacheInitialization;

/*!
 @method
 @abstract 清除所有url缓存
 @discussion
 @result
 */
- (void)clearAllURLCache;

/*!
 @method
 @abstract 发送Http请求
 @discussion
 @param aRequestModel 请求数据model
 @param aSuccessBlock 成功block
 @param　aFailureBlock　失败block
 @result
 */
-(void)sendHttpRequestWithModel:(BDMHTTPRequestModel *) aRequestModel
                        success:(void (^)(id responseObject))aSuccessBlock
                        failure:(void (^)(BDMError *error))aFailureBlock;

/*!
 @method
 @abstract 批量发送Http请求
 @discussion
 @param modelArray 请求数据model数组
 @param progress 每个请求执行的回调
 @param complete 所有请求执行完后的回调
 @param　isResultRestrict　结果是否严格限制（yes表示每有一个请求失败立刻执行complete，
        no 表示单个的请求失败并不影响整体的回调，依然会等最后一个回调执行后再执行complete）
 @result
 */
- (void)sendHttpRequestGroupWithModelArray:(NSArray<BDMHTTPRequestModel *> *)modelArray
                                  progress:(void(^)(id responseObject, NSString *identifier, BDMError *error))progress
                                  complete:(void(^)(BOOL isRequestSuccess))complete
                          isResultRestrict:(BOOL)isResultRestrict;

/**
 *  @brief  上传文件时调用方法
 *
 *  @param aUploadFileModel 上传数据的model
 *  @param aProgressBlock   上传进度block
 *  @param aSuccessBlock    成功block
 *  @param aFailureBlock    失败block
 */
- (void)uploadFileWithFileModel:(BDMUploadFileModel *)aUploadFileModel
               progressCallBack:(void (^)(NSUInteger __unused bytesWritten,
                                          long long totalBytesWritten,
                                          long long totalBytesExpectedToWrite))progressBlock
                        success:(void (^)(id responseObject))aSuccessBlock
                        failure:(void (^)(NSError *error))aFailureBlock;
/**
 *  @brief  下载文件时调用方法
 *
 *  @param aDownloadModel 下载数据的model
 *  @param progressBlock  下载进度block
 *  @param aSuccessBlock  成功block
 *  @param aFailureBlock  失败block
 */
- (void)downloadFileWithFileModel:(BDMDownloadFileModel *)aDownloadModel
                 progressCallBack:(void (^)(NSUInteger bytesRead,
                                            long long totalBytesRead,
                                            long long totalBytesExpectedToRead))progressBlock
                          success:(void (^)(id responseObject))aSuccessBlock
                          failure:(void (^)(NSError *error))aFailureBlock;

- (void)cancelRequestForIdentifier:(NSString *)requestID;
- (void)cancelRequestForIdentifierList:(NSMutableArray *)requestIDList;
- (void)cancelAllRequest;
@end
